package cn.datawin.routeserver.input.action;

import cn.datawin.bms.customer.entity.CstmCustomerEntity;
import cn.datawin.bms.customer.redis.CstmCustomerCmppRedisDto;
import cn.datawin.common.core.util.RedisUtils;
import cn.datawin.constant.ApiStatus;
import cn.datawin.constant.RedisPattern;
import cn.datawin.routeserver.input.machine.Basket;
import cn.datawin.routeserver.input.service.ErrorReportService;
import cn.datawin.routeserver.service.CstmCustomerService;
import cn.datawin.sms.cmpp2.SmsDetailLog;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.squirrelframework.foundation.fsm.UntypedAnonymousAction;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;

@Slf4j
public class CheckAction extends BaseAction {
	@Override
	public void execute(Object ruleFrom, Object ruleTo, Object event, Object context, UntypedStateMachine stateMachine) {
		Basket basket = (Basket) context;
		SmsDetailLog smsDetailLog = basket.getSmsDetailLog();
		//1、检测用户是否停用
//		CstmCustomerService cstmCustomerService = SpringUtil.getBean(CstmCustomerService.class);
		String rKey = String.format(RedisPattern.CSTM_CUSTOMER_CMPP_CACHE, smsDetailLog.getCstmCustomerCode());
		CstmCustomerCmppRedisDto cstmCustomerCmppRedisDto = RedisUtils.get(rKey);

		if (cstmCustomerCmppRedisDto == null) {
			log.error("用户不存在");
			reportError(basket,ApiStatus._10001, smsDetailLog);
			return;
		}
		basket.setCstmCustomerCmppRedisDto(cstmCustomerCmppRedisDto);
		smsDetailLog.setCstmCustomerId(cstmCustomerCmppRedisDto.getCustomerId());

		if (cstmCustomerCmppRedisDto.getStatus() == 0) {
			log.error("用户已停用");
			reportError(basket,ApiStatus._10002, smsDetailLog);
			return;
		}

		//2、检测手机号是否格式正确,利用hutool的手机号工具类
		String phoneNos = smsDetailLog.getPhoneNos();
		if (!PhoneUtil.isMobile(phoneNos)) {
			log.error("手机号格式错误");
			reportError(basket,ApiStatus._10003, smsDetailLog);
			return;
		}
		//如果以86开头，去掉86
		if (phoneNos.startsWith("86")) {
			phoneNos = phoneNos.substring(2);
		}

		//3、获取归属地 设置provinceCode，方便后续选取通道
		int prefix = Integer.parseInt(phoneNos.substring(0,3));
		int suffix = Integer.parseInt(phoneNos.substring(3));

		String redisKey = String.format(RedisPattern.BASE_AREA_NUMBER_PREFIX, prefix);
		String hget = RedisUtils.hget(redisKey, suffix + "");
		JSONObject jsonObject = JSON.parseObject(hget);
		String provinceCode = jsonObject.getString("provinceCode");
		String cityCode = jsonObject.getString("cityCode");
		String ispCode = jsonObject.getString("ispCode");
		//3、设置provinceCode，方便后续选取通道
		smsDetailLog.setProvince(provinceCode);
		smsDetailLog.setCity(cityCode);
		smsDetailLog.setIspCode(ispCode);
		//4、携号转网更改ispCode
		String redisKey1 = String.format(RedisPattern.BASE_NUMBER_PORTING_PREFIX, prefix);
		String hget1 = RedisUtils.hget(redisKey1, suffix + "");
		if(StrUtil.isNotBlank(hget1)){//替换成携号转网的ispCode
			smsDetailLog.setIspCode(hget1);
		}

	}



}
